#include "stdafx.h"
#include "LoAg.h"

#define DISPID_QUIT			1
#define DISPID_CLOSEWINDOW	2

class LoAgTestIDispatch : public IDispatch
{
public:
	LoAgTestIDispatch()
	{
		comReferenceCount = 0;
	}
	virtual HRESULT STDMETHODCALLTYPE QueryInterface( REFIID riid, __RPC__deref_out void __RPC_FAR *__RPC_FAR *ppvObject )
	{
		return E_NOTIMPL;
	}
	ULONG _stdcall AddRef()
	{
		return ++comReferenceCount;
	}
	ULONG _stdcall Release()
	{
		if( --comReferenceCount == 0 )
		{
			//delete this; // This is a singleton
		}
		return comReferenceCount;
	}
	virtual HRESULT STDMETHODCALLTYPE GetTypeInfoCount(  /* [out] */ __RPC__out UINT *pctinfo )
	{
		*pctinfo = 0;
		return E_NOTIMPL;
	}
	virtual HRESULT STDMETHODCALLTYPE GetTypeInfo( /* [in] */ UINT iTInfo, /* [in] */ LCID lcid, /* [out] */ __RPC__deref_out_opt ITypeInfo **ppTInfo )
	{
		return E_NOTIMPL;
	}
	virtual HRESULT STDMETHODCALLTYPE GetIDsOfNames( /* [in] */ __RPC__in REFIID riid,
			/* [size_is][in] */ __RPC__in_ecount_full(cNames) LPOLESTR *rgszNames, /* [range][in] */ UINT cNames,
			/* [in] */ LCID lcid, /* [size_is][out] */ __RPC__out_ecount_full(cNames) DISPID *rgDispId)
	{
		HRESULT	hErr = S_OK;
		for( UINT i = 0; i < cNames; ++i )
		{
			if( 0 == lstrcmpi( rgszNames[i], L"Quit" ) )
			{
				rgDispId[i] = DISPID_QUIT;
			}
			else
			{
				rgDispId[i] = DISPID_UNKNOWN;
				hErr = DISP_E_UNKNOWNNAME;
			}
		}
		return hErr;
	}
	virtual /* [local] */ HRESULT STDMETHODCALLTYPE Invoke( /* [in] */ DISPID dispIdMember, /* [in] */ REFIID riid,
			/* [in] */ LCID lcid, /* [in] */ WORD wFlags, /* [out][in] */ DISPPARAMS *pDispParams,
			/* [out] */ VARIANT *pVarResult, /* [out] */ EXCEPINFO *pExcepInfo, /* [out] */ UINT *puArgErr )
	{
		HRESULT	hErr = E_NOTIMPL;
		switch( dispIdMember )
		{
		case DISPID_QUIT:
			hErr = Quit();
			break;
		}
		return hErr;
	}
	// Call table
	HRESULT STDMETHODCALLTYPE Quit()
	{
		PostQuitMessage(0);
		return S_OK;
	}
	// Change Icon
	HRESULT STDMETHODCALLTYPE ChangeTrayIcon( wchar_t const *iconfile )
	{
		return S_OK;
	}
private:
	ULONG comReferenceCount;
};

LoAgTestIDispatch	exampleAgIDispatch;
extern IDispatch	*iDispatchSilverlightInterface = (IDispatch *)(&exampleAgIDispatch);

